<template>
  <el-dialog
    :title="!dataForm.tbOfficeBuildingId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="封面图片" prop="tbOfficeBuildingPicture">
      <el-input v-model="dataForm.tbOfficeBuildingPicture" placeholder="封面图片"></el-input>
    </el-form-item>
    <el-form-item label="标题" prop="tbOfficeBuildingHeadling">
      <el-input v-model="dataForm.tbOfficeBuildingHeadling" placeholder="标题"></el-input>
    </el-form-item>
    <el-form-item label="标签" prop="tbOfficeBuildingLabel">
      <el-input v-model="dataForm.tbOfficeBuildingLabel" placeholder="标签"></el-input>
    </el-form-item>
    <el-form-item label="地区" prop="tbOfficeBuildingRegion">
      <el-input v-model="dataForm.tbOfficeBuildingRegion" placeholder="地区"></el-input>
    </el-form-item>
    <el-form-item label="建筑面积" prop="tbOfficeBuildingArea">
      <el-input v-model="dataForm.tbOfficeBuildingArea" placeholder="建筑面积"></el-input>
    </el-form-item>
    <el-form-item label="月租" prop="tbOfficeBuildingMonthlyrent">
      <el-input v-model="dataForm.tbOfficeBuildingMonthlyrent" placeholder="月租"></el-input>
    </el-form-item>
    <el-form-item label="总价" prop="tbOfficeBuildingTotal">
      <el-input v-model="dataForm.tbOfficeBuildingTotal" placeholder="总价"></el-input>
    </el-form-item>
    <el-form-item label="装修" prop="tbOfficeBuildingFitment">
      <el-input v-model="dataForm.tbOfficeBuildingFitment" placeholder="装修"></el-input>
    </el-form-item>
    <el-form-item label="写字楼类型" prop="tbOfficeBuildingType">
      <el-input v-model="dataForm.tbOfficeBuildingType" placeholder="写字楼类型"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="tbOfficeBuildingTurnovertime">
      <el-input v-model="dataForm.tbOfficeBuildingTurnovertime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="置顶" prop="tbOfficeBuildingStick">
      <el-input v-model="dataForm.tbOfficeBuildingStick" placeholder="置顶"></el-input>
    </el-form-item>
    <el-form-item label="地区id" prop="tbAreaId">
      <el-input v-model="dataForm.tbAreaId" placeholder="地区id"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          tbOfficeBuildingId: 0,
          tbOfficeBuildingPicture: '',
          tbOfficeBuildingHeadling: '',
          tbOfficeBuildingLabel: '',
          tbOfficeBuildingRegion: '',
          tbOfficeBuildingArea: '',
          tbOfficeBuildingMonthlyrent: '',
          tbOfficeBuildingTotal: '',
          tbOfficeBuildingFitment: '',
          tbOfficeBuildingType: '',
          tbOfficeBuildingTurnovertime: '',
          tbOfficeBuildingStick: '',
          tbAreaId: ''
        },
        dataRule: {
          tbOfficeBuildingPicture: [
            { required: true, message: '封面图片不能为空', trigger: 'blur' }
          ],
          tbOfficeBuildingHeadling: [
            { required: true, message: '标题不能为空', trigger: 'blur' }
          ],
          tbOfficeBuildingLabel: [
            { required: true, message: '标签不能为空', trigger: 'blur' }
          ],
          tbOfficeBuildingRegion: [
            { required: true, message: '地区不能为空', trigger: 'blur' }
          ],
          tbOfficeBuildingArea: [
            { required: true, message: '建筑面积不能为空', trigger: 'blur' }
          ],
          tbOfficeBuildingMonthlyrent: [
            { required: true, message: '月租不能为空', trigger: 'blur' }
          ],
          tbOfficeBuildingTotal: [
            { required: true, message: '总价不能为空', trigger: 'blur' }
          ],
          tbOfficeBuildingFitment: [
            { required: true, message: '装修不能为空', trigger: 'blur' }
          ],
          tbOfficeBuildingType: [
            { required: true, message: '写字楼类型不能为空', trigger: 'blur' }
          ],
          tbOfficeBuildingTurnovertime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          tbOfficeBuildingStick: [
            { required: true, message: '置顶不能为空', trigger: 'blur' }
          ],
          tbAreaId: [
            { required: true, message: '地区id不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.tbOfficeBuildingId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.tbOfficeBuildingId) {
            this.$http({
              url: this.$http.adornUrl(`/twohouse/officebuilding/info/${this.dataForm.tbOfficeBuildingId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.tbOfficeBuildingPicture = data.officeBuilding.tbOfficeBuildingPicture
                this.dataForm.tbOfficeBuildingHeadling = data.officeBuilding.tbOfficeBuildingHeadling
                this.dataForm.tbOfficeBuildingLabel = data.officeBuilding.tbOfficeBuildingLabel
                this.dataForm.tbOfficeBuildingRegion = data.officeBuilding.tbOfficeBuildingRegion
                this.dataForm.tbOfficeBuildingArea = data.officeBuilding.tbOfficeBuildingArea
                this.dataForm.tbOfficeBuildingMonthlyrent = data.officeBuilding.tbOfficeBuildingMonthlyrent
                this.dataForm.tbOfficeBuildingTotal = data.officeBuilding.tbOfficeBuildingTotal
                this.dataForm.tbOfficeBuildingFitment = data.officeBuilding.tbOfficeBuildingFitment
                this.dataForm.tbOfficeBuildingType = data.officeBuilding.tbOfficeBuildingType
                this.dataForm.tbOfficeBuildingTurnovertime = data.officeBuilding.tbOfficeBuildingTurnovertime
                this.dataForm.tbOfficeBuildingStick = data.officeBuilding.tbOfficeBuildingStick
                this.dataForm.tbAreaId = data.officeBuilding.tbAreaId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/twohouse/officebuilding/${!this.dataForm.tbOfficeBuildingId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'tbOfficeBuildingId': this.dataForm.tbOfficeBuildingId || undefined,
                'tbOfficeBuildingPicture': this.dataForm.tbOfficeBuildingPicture,
                'tbOfficeBuildingHeadling': this.dataForm.tbOfficeBuildingHeadling,
                'tbOfficeBuildingLabel': this.dataForm.tbOfficeBuildingLabel,
                'tbOfficeBuildingRegion': this.dataForm.tbOfficeBuildingRegion,
                'tbOfficeBuildingArea': this.dataForm.tbOfficeBuildingArea,
                'tbOfficeBuildingMonthlyrent': this.dataForm.tbOfficeBuildingMonthlyrent,
                'tbOfficeBuildingTotal': this.dataForm.tbOfficeBuildingTotal,
                'tbOfficeBuildingFitment': this.dataForm.tbOfficeBuildingFitment,
                'tbOfficeBuildingType': this.dataForm.tbOfficeBuildingType,
                'tbOfficeBuildingTurnovertime': this.dataForm.tbOfficeBuildingTurnovertime,
                'tbOfficeBuildingStick': this.dataForm.tbOfficeBuildingStick,
                'tbAreaId': this.dataForm.tbAreaId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
